package com.xaicode.template.activemq.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class JMS_consumer_topic_persist {


    public static final String ACTIVEMQ_URL = "tcp://192.168.18.182:61616";
    public static final String TOPIC_NAME = "CHECK_YARD_TOPIC";


    /**
     * 1、一次运行订阅此公众号 注册一次 然后关闭程序
     * 2、再次运行依然可以收到持久化消息
     *
     * @param args
     * @throws JMSException
     */
    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.setClientID("z3");
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        Topic topic = session.createTopic(TOPIC_NAME);
        TopicSubscriber durableSubscriber = session.createDurableSubscriber(topic, "remark...");
        connection.start();
        Message message = durableSubscriber.receive();
        while (null != message) {
            TextMessage textMessage = (TextMessage) message;
            System.out.println("收到持久化消息" + textMessage.getText());
            message = durableSubscriber.receive();
        }
        session.close();
        connection.close();
    }
}
